package de.app.badresden;

import login.LoginActivity;
import mensaplan.MensaActivity;
import news.NewsActivity;
import noten.NotenActivity;
import settings.SettingsActivity;
import stundenplan.StundenplanActivity;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class StartScreenActivity extends Activity implements OnClickListener {

	private ImageButton notenButton;
	private ImageButton mensaplanButton;
	private ImageButton stundenplanButton;
	private ImageButton newsButton;
	private ImageButton settingsButton;
	private ImageButton facebookButton;
	private String cookievalue = "";
	private SharedPreferences preferences;

	public void onCreate(Bundle icicle) {

		super.onCreate(icicle);

		preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

		deleteCookie();
		setContentView(R.layout.startscreen_layout);

		Display display = getWindowManager().getDefaultDisplay(); 
		int width = display.getWidth();

		int margin = (width-260)/4;
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		lp.setMargins(margin, margin, margin, 0);
				
		notenButton = (ImageButton) findViewById(R.id.startscreen_notenBtn);
		notenButton.setLayoutParams(lp);
		notenButton.setOnClickListener(this);
		stundenplanButton = (ImageButton) findViewById(R.id.startscreen_stundenplanBtn);
		stundenplanButton.setLayoutParams(lp);
		stundenplanButton.setOnClickListener(this);
		mensaplanButton = (ImageButton) findViewById(R.id.startscreen_mensaplanBtn);
		mensaplanButton.setLayoutParams(lp);
		mensaplanButton.setOnClickListener(this);
		newsButton = (ImageButton) findViewById(R.id.startscreen_newsBtn);
		newsButton.setLayoutParams(lp);
		newsButton.setOnClickListener(this);
		settingsButton = (ImageButton) findViewById(R.id.startscreen_settingsBtn);
		settingsButton.setLayoutParams(lp);
		settingsButton.setOnClickListener(this);
		facebookButton = (ImageButton) findViewById(R.id.startscreen_facebookBtn);
		facebookButton.setLayoutParams(lp);
		facebookButton.setOnClickListener(this);
	}

	@Override
	protected void onResume() {

		super.onResume();
		if (cookievalue.equals("")) cookievalue = preferences.getString("cookie", "");
		onCreate(new Bundle());
	}

	private boolean checkInternet() {

		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}

	public void onClick(View clickedButton) {

		Bundle bundle = new Bundle();
		boolean hasInternet = checkInternet();
		bundle.putBoolean("HasInternet", hasInternet);

		if (clickedButton == notenButton) {
			final Intent intent = new Intent();
			if (!hasInternet) intent.setClass(this, NotenActivity.class);

			else if (cookievalue.length() == 0) {
				intent.setClass(this, LoginActivity.class);
				bundle.putString("loginreason", "noten");
			}
			else {
				bundle.putString("cookievalue", cookievalue);
				intent.setClass(this, NotenActivity.class);
			}
			intent.putExtras(bundle);
			startActivity(intent);
		}

		if (clickedButton == stundenplanButton) {
			final Intent intent = new Intent();
			if (!hasInternet) intent.setClass(this, StundenplanActivity.class);

			else if (cookievalue.length() == 0) {
				intent.setClass(this, LoginActivity.class);
				bundle.putString("loginreason", "stundenplan");
			}
			else {
				bundle.putString("cookievalue", cookievalue);
				intent.setClass(this, StundenplanActivity.class);
			}
			intent.putExtras(bundle);
			startActivity(intent);
		}
		if (clickedButton == newsButton) {
			final Intent intent = new Intent();
			if (!hasInternet) intent.setClass(this, NewsActivity.class);

			else if (cookievalue.length() == 0) {
				intent.setClass(this, LoginActivity.class);
				bundle.putString("loginreason", "news");
			}
			else {
				bundle.putString("cookievalue", cookievalue);
				intent.setClass(this, NewsActivity.class);
			}
			intent.putExtras(bundle);
			startActivity(intent);
		}
		if (clickedButton == mensaplanButton) {
			if (!hasInternet) showNoInternetDialog();
			else {
				final Intent intent = new Intent(this, MensaActivity.class);
				startActivity(intent);
			}
		}
		if (clickedButton == settingsButton) {
			final Intent intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
		}
		if (clickedButton == facebookButton) {
			if (!hasInternet) showNoInternetDialog();
			else {
				Uri uri = Uri.parse(getString(R.string.url_facebook));
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		}
	}

	private void showNoInternetDialog() {

		final Dialog dialog = new Dialog(StartScreenActivity.this);
		dialog.setCancelable(false);
		dialog.setContentView(R.layout.login_dialoglayout);
		dialog.setTitle(getString(R.string.app_achtung));

		TextView text = (TextView) dialog.findViewById(R.id.hinweisdialog_text);
		text.setText(getString(R.string.app_keinInternet));

		Button m = (Button) dialog.findViewById(R.id.hinweisdialog_button);
		m.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				dialog.cancel();
			}
		});
		dialog.show();

	}

	private void deleteCookie() {

		Editor edit = preferences.edit();
		edit.putString("cookie", "");
		edit.commit();
	}
}
